/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package backupit.gui.control;

import backupit.gui.*;
import backupit.gui.control.BackupWriterController;
import backupit.GUILauncher;
import backupit.core.dirbackup.DirBackup;
import backupit.core.dirbackup.io.DirBackupWriter;
import java.awt.Component;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import javax.swing.JProgressBar;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author dbotelho
 */
public class BackupWriterManagerController {
    private LinkedList<DirBackupWriter> backList;
    private GUILauncher launcher;
    private BackupWriterManagerDialog backupManager;

    public BackupWriterManagerController(GUILauncher launcher){
        this.backList = new LinkedList<DirBackupWriter>();
        this.launcher = launcher;
        
        backupManager = new BackupWriterManagerDialog(launcher.getMainFrame(),false);
    }
    public void setVisible(boolean visible){
        this.getBackupManagerGUI().setVisible(visible);
    }
    
    public boolean execute(DirBackup backup) throws IOException {
        
        DirBackupWriter b = new DirBackupWriter(backup);
        if (!this.backList.add(b)) 
            return false;
        new Thread(b).start();
        return true;
    }
    
    /**
     * This method should be called by the MainFrame, because it's going to use backupTabel that
     * it's in it.
     * 
     * @param tableIdx the index of the the backup row thats going to be backuped
     * @param backup The 
     * @return
     * @throws java.io.IOException
     */
    public boolean execute(int tableIdx, DirBackup backup) throws IOException{
        new Thread(new BackupWriterController(launcher, 
                (JProgressBar) launcher.getMainFrame().getBackupTable().getValueAt(tableIdx, launcher.getMainFrame().getBackupTable().getColumnModel().getColumnIndex(launcher.getMainFrame().STATUS_COLUMN)), backup)).start();
        return true;
    }
    
    public void updateUI(String looknFeel) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException{
        backupManager.updateUI(looknFeel);
    }

    public BackupWriterManagerDialog getBackupManagerGUI() {
        return backupManager;
    }

}
